package com.fr.report;

import com.fr.base.core.list.FRCacheList;
import com.fr.report.core.ReportUtils;
import java.awt.Rectangle;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

public class DefaultCellCase
  implements CellCase
{
  private List rowList;
  private int columnCount;

  public DefaultCellCase()
  {
    this(1, 1);
  }

  public DefaultCellCase(int paramInt1, int paramInt2)
  {
    this.rowList = new ArrayList(paramInt1);
    for (int i = 0; i < paramInt1; ++i)
      this.rowList.add(new Row(paramInt2));
    this.columnCount = paramInt2;
  }

  public void toCache(int paramInt1, int paramInt2, boolean paramBoolean)
  {
    if (!(this.rowList instanceof FRCacheList))
    {
      List localList = this.rowList;
      this.rowList = new FRCacheList(paramInt1, paramInt2, "CE", paramBoolean);
      this.rowList.addAll(localList);
      localList.clear();
    }
  }

  public void releaseCache()
  {
    if (this.rowList instanceof FRCacheList)
      this.rowList.clear();
  }

  public Iterator cellIterator()
  {
    return new CellIterator(this);
  }

  public Iterator intersect(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
  {
    return new IntersectIterator(this, paramInt1, paramInt2, paramInt3, paramInt4, null);
  }

  public Iterator getColumn(int paramInt)
  {
    return new ColumnIterator(this, paramInt);
  }

  public Iterator getRow(int paramInt)
  {
    return new RowIterator(this, paramInt);
  }

  public int getRowCount()
  {
    return this.rowList.size();
  }

  public int getColumnCount()
  {
    return this.columnCount;
  }

  public void add(Cell paramCell, boolean paramBoolean)
  {
    int i1;
    if (paramCell == null)
      return;
    int i = paramCell.getRow();
    int j = paramCell.getRowSpan();
    int k = paramCell.getColumn();
    int l = paramCell.getColumnSpan();
    if ((i < 0) || (j <= 0) || (k < 0) || (l <= 0))
      return;
    if (paramBoolean)
      for (i1 = 0; i1 < j; ++i1)
        for (int i2 = 0; i2 < l; ++i2)
          removeCell(k + i2, i + i1);
    __nailCellElement(paramCell);
    this.columnCount = Math.max(this.columnCount, k + l);
  }

  public Cell get(int paramInt1, int paramInt2)
  {
    if ((paramInt2 >= this.rowList.size()) || (paramInt2 < 0))
      return null;
    Row localRow = (Row)this.rowList.get(paramInt2);
    return localRow.getElement(paramInt1);
  }

  public Cell removeCell(int paramInt1, int paramInt2)
  {
    Cell localCell = get(paramInt1, paramInt2);
    if (__liftCellElement(localCell))
    {
      if (localCell.getColumn() + localCell.getColumnSpan() == this.columnCount)
        recalculateColumnCount();
      if (localCell.getRow() + localCell.getRowSpan() == this.rowList.size())
        recalculateRowCount();
      return localCell;
    }
    return null;
  }

  public void clear()
  {
    this.rowList.clear();
    this.columnCount = 0;
  }

  public void insertColumn(int paramInt)
  {
    int i = 0;
    int j = this.rowList.size();
    while (i < j)
    {
      Row localRow = (Row)this.rowList.get(i);
      for (int k = localRow.width() - 1; k >= paramInt; --k)
      {
        Cell localCell2 = localRow.getElement(k);
        if ((localCell2 != null) && (localCell2.getColumn() == k) && (localCell2.getRow() == i))
          localCell2.setColumn(localCell2.getColumn() + 1);
      }
      Cell localCell1 = localRow.getElement(paramInt);
      localRow.insert(paramInt);
      if ((localCell1 != null) && (localCell1.getColumn() < paramInt))
      {
        if (localCell1.getRow() == i)
          localCell1.setColumnSpan(localCell1.getColumnSpan() + 1);
        localRow.setElement(paramInt, localCell1);
      }
      ++i;
    }
    this.columnCount += 1;
  }

  public void insertRow(int paramInt)
  {
    Row localRow2;
    int j;
    int k;
    Cell localCell;
    if (paramInt > this.rowList.size())
      return;
    for (int i = this.rowList.size() - 1; i >= paramInt; --i)
    {
      localRow2 = (Row)this.rowList.get(i);
      j = 0;
      k = localRow2.width();
      while (j < k)
      {
        localCell = localRow2.getElement(j);
        if ((localCell != null) && (localCell.getColumn() == j) && (localCell.getRow() == i))
          localCell.setRow(localCell.getRow() + 1);
        ++j;
      }
    }
    Row localRow1 = new Row();
    if (paramInt < this.rowList.size())
    {
      localRow2 = (Row)this.rowList.get(paramInt);
      j = 0;
      k = localRow2.width();
      while (j < k)
      {
        localCell = localRow2.getElement(j);
        if ((localCell != null) && (localCell.getRow() < paramInt))
        {
          if (localCell.getColumn() == j)
            localCell.setRowSpan(localCell.getRowSpan() + 1);
          localRow1.setElement(j, localCell);
        }
        ++j;
      }
    }
    this.rowList.add(paramInt, localRow1);
  }

  public int removeColumn(int paramInt)
  {
    Row localRow;
    int i = 0;
    if ((paramInt >= this.columnCount) || (paramInt < 0))
      return i;
    int j = 0;
    int k = this.rowList.size();
    while (j < k)
    {
      localRow = (Row)this.rowList.get(j);
      Cell localCell1 = localRow.getElement(paramInt);
      if (localCell1 == null)
        break label191:
      if ((localCell1.getColumn() == paramInt) && (localCell1.getRow() == j))
      {
        __liftCellElement(localCell1);
        if (localCell1.getColumnSpan() > 1)
        {
          localCell1.setColumn(localCell1.getColumn() + 1);
          localCell1.setColumnSpan(localCell1.getColumnSpan() - 1);
          __nailCellElement(localCell1);
        }
        else
        {
          ++i;
        }
      }
      else if ((localCell1.getColumn() < paramInt) && (localCell1.getRow() == j))
      {
        localCell1.setColumnSpan(localCell1.getColumnSpan() - 1);
      }
      label191: ++j;
    }
    j = 0;
    k = this.rowList.size();
    while (j < k)
    {
      localRow = (Row)this.rowList.get(j);
      int l = paramInt + 1;
      int i1 = localRow.width();
      while (l < i1)
      {
        Cell localCell2 = localRow.getElement(l);
        if ((localCell2 != null) && (localCell2.getColumn() == l) && (localCell2.getRow() == j))
          localCell2.setColumn(localCell2.getColumn() - 1);
        ++l;
      }
      ++j;
    }
    j = 0;
    k = this.rowList.size();
    while (j < k)
    {
      ((Row)this.rowList.get(j)).removeColumn(paramInt);
      ++j;
    }
    this.columnCount -= 1;
    return i;
  }

  public int removeRow(int paramInt)
  {
    int i = 0;
    if ((paramInt >= this.rowList.size()) || (paramInt < 0))
      return i;
    Row localRow = (Row)this.rowList.get(paramInt);
    int j = 0;
    int k = localRow.width();
    while (j < k)
    {
      Cell localCell1 = localRow.getElement(j);
      if (localCell1 == null)
        break label194:
      if ((localCell1.getColumn() == j) && (localCell1.getRow() == paramInt))
      {
        __liftCellElement(localCell1);
        if (localCell1.getRowSpan() > 1)
        {
          localCell1.setRow(localCell1.getRow() + 1);
          localCell1.setRowSpan(localCell1.getRowSpan() - 1);
          __nailCellElement(localCell1);
        }
        else
        {
          ++i;
        }
      }
      else if ((localCell1.getRow() < paramInt) && (localCell1.getColumn() == j))
      {
        localCell1.setRowSpan(localCell1.getRowSpan() - 1);
      }
      label194: ++j;
    }
    j = paramInt + 1;
    k = this.rowList.size();
    while (j < k)
    {
      localRow = (Row)this.rowList.get(j);
      int l = 0;
      int i1 = localRow.width();
      while (l < i1)
      {
        Cell localCell2 = localRow.getElement(l);
        if ((localCell2 != null) && (localCell2.getColumn() == l) && (localCell2.getRow() == j))
          localCell2.setRow(localCell2.getRow() - 1);
        ++l;
      }
      ++j;
    }
    this.rowList.remove(paramInt);
    return i;
  }

  public void recalculate()
  {
    recalculateRowCount();
    recalculateColumnCount();
  }

  private void recalculateColumnCount()
  {
    int i = 0;
    int j = 0;
    int k = this.rowList.size();
    while (j < k)
    {
      i = Math.max(i, ((Row)this.rowList.get(j)).getNotNullWidth());
      ++j;
    }
    this.columnCount = i;
  }

  private void recalculateRowCount()
  {
    for (int i = this.rowList.size() - 1; (i >= 0) && (((Row)this.rowList.get(i)).getNotNullWidth() == 0); --i)
      this.rowList.remove(i);
  }

  private void ensureCapacity(int paramInt)
  {
    while (paramInt > this.rowList.size())
      this.rowList.add(new Row());
  }

  public void __nailCellElement(Cell paramCell)
  {
    int i = paramCell.getRow();
    int j = paramCell.getColumn();
    ensureCapacity(i + paramCell.getRowSpan());
    int k = 0;
    int l = paramCell.getRowSpan();
    while (k < l)
    {
      Row localRow = (Row)this.rowList.get(i + k);
      int i1 = 0;
      int i2 = paramCell.getColumnSpan();
      while (i1 < i2)
      {
        localRow.setElement(j + i1, paramCell);
        ++i1;
      }
      ++k;
    }
  }

  public boolean __liftCellElement(Cell paramCell)
  {
    if (paramCell == null)
      return false;
    int i = paramCell.getRow();
    int j = paramCell.getColumn();
    int k = 0;
    int l = paramCell.getRowSpan();
    while (k < l)
    {
      Row localRow = (Row)this.rowList.get(i + k);
      int i1 = 0;
      int i2 = paramCell.getColumnSpan();
      while (i1 < i2)
      {
        if (localRow.getElement(j + i1) == paramCell)
          localRow.setElement(j + i1, null);
        ++i1;
      }
      ++k;
    }
    return true;
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    DefaultCellCase localDefaultCellCase = (DefaultCellCase)super.clone();
    localDefaultCellCase.rowList = new ArrayList(this.rowList.size());
    Iterator localIterator = cellIterator();
    while (localIterator.hasNext())
    {
      CellElement localCellElement = (CellElement)localIterator.next();
      localDefaultCellCase.__nailCellElement((CellElement)localCellElement.clone());
    }
    return localDefaultCellCase;
  }

  static List access$100(DefaultCellCase paramDefaultCellCase)
  {
    return paramDefaultCellCase.rowList;
  }

  static int access$200(DefaultCellCase paramDefaultCellCase)
  {
    return paramDefaultCellCase.columnCount;
  }

  private class RowIterator
    implements Iterator
  {
    private int rowIndex;
    int current_column;
    int next_column;
    Cell next;
    private final DefaultCellCase this$0;

    public RowIterator(, int paramInt)
    {
      this.this$0 = paramDefaultCellCase;
      this.current_column = -1;
      this.next_column = 0;
      this.rowIndex = paramInt;
      findNext();
    }

    public boolean hasNext()
    {
      return (this.next != null);
    }

    public Object next()
    {
      if (!(hasNext()))
        throw new NoSuchElementException("At last element");
      Cell localCell = this.next;
      findNext();
      return localCell;
    }

    public void remove()
    {
      this.this$0.removeCell(this.current_column, this.rowIndex);
    }

    private void findNext()
    {
      Cell localCell;
      if ((this.rowIndex < 0) || (this.rowIndex >= DefaultCellCase.access$100(this.this$0).size()))
        return;
      DefaultCellCase.Row localRow = (DefaultCellCase.Row)DefaultCellCase.access$100(this.this$0).get(this.rowIndex);
      do
      {
        if (this.next_column >= localRow.width())
          break label121;
        localCell = localRow.getElement(this.next_column);
        this.current_column = this.next_column;
        this.next_column += 1;
      }
      while ((localCell == null) || (localCell.getColumn() != this.current_column) || (localCell.getRow() != this.rowIndex));
      this.next = localCell;
      return;
      label121: this.next = null;
    }
  }

  private class ColumnIterator
    implements Iterator
  {
    private int columnIndex;
    private int current_row;
    private int next_row;
    private Cell next;
    private final DefaultCellCase this$0;

    ColumnIterator(, int paramInt)
    {
      this.this$0 = paramDefaultCellCase;
      this.current_row = -1;
      this.next_row = 0;
      this.columnIndex = paramInt;
      findNext();
    }

    public boolean hasNext()
    {
      return (this.next != null);
    }

    public Object next()
    {
      if (!(hasNext()))
        throw new NoSuchElementException("At last element");
      Cell localCell = this.next;
      findNext();
      return localCell;
    }

    public void remove()
    {
      this.this$0.removeCell(this.columnIndex, this.current_row);
    }

    private void findNext()
    {
      Cell localCell;
      do
      {
        if (this.next_row >= DefaultCellCase.access$100(this.this$0).size())
          break label100;
        localCell = ((DefaultCellCase.Row)DefaultCellCase.access$100(this.this$0).get(this.next_row)).getElement(this.columnIndex);
        this.current_row = this.next_row;
        this.next_row += 1;
      }
      while ((localCell == null) || (localCell.getColumn() != this.columnIndex) || (localCell.getRow() != this.current_row));
      this.next = localCell;
      return;
      label100: this.next = null;
    }
  }

  private class IntersectIterator
    implements Iterator
  {
    private int columnFrom;
    private int columnTo;
    private int rowFrom;
    private int rowTo;
    private int thisrow;
    private int thiscol;
    private int nextrow;
    private int nextcol;
    Cell next;
    private final DefaultCellCase this$0;

    private IntersectIterator(, int paramInt1, int paramInt2, int paramInt3, int paramInt4)
    {
      this.this$0 = paramDefaultCellCase;
      this.next = null;
      this.columnFrom = Math.max(paramInt1, 0);
      this.columnTo = Math.min(paramInt1 + paramInt3 - 1, DefaultCellCase.access$200(paramDefaultCellCase) - 1);
      this.rowFrom = Math.max(paramInt2, 0);
      this.rowTo = Math.min(paramInt2 + paramInt4 - 1, DefaultCellCase.access$100(paramDefaultCellCase).size() - 1);
      this.thisrow = -1;
      this.thiscol = -1;
      this.nextrow = this.rowFrom;
      this.nextcol = (this.columnFrom - 1);
      findNext();
    }

    public boolean hasNext()
    {
      return (this.next != null);
    }

    public Object next()
    {
      if (!(hasNext()))
        throw new NoSuchElementException("At last element");
      Cell localCell = this.next;
      this.thiscol = this.nextcol;
      this.thisrow = this.nextrow;
      findNext();
      return localCell;
    }

    public void remove()
    {
      this.this$0.removeCell(this.thiscol, this.thisrow);
    }

    private void findNext()
    {
      int i = this.nextcol + 1;
      for (int j = this.nextrow; j <= this.rowTo; ++j)
      {
        if (DefaultCellCase.access$100(this.this$0).size() <= j)
          break;
        DefaultCellCase.Row localRow = (DefaultCellCase.Row)DefaultCellCase.access$100(this.this$0).get(j);
        while (i <= this.columnTo)
        {
          Cell localCell = localRow.getElement(i);
          if (localCell == null)
            break label199:
          Rectangle localRectangle = ReportUtils.intersection(localCell.getColumn(), localCell.getColumn() + localCell.getColumnSpan(), localCell.getRow(), localCell.getRow() + localCell.getRowSpan(), this.columnFrom, this.columnTo + 1, this.rowFrom, this.rowTo + 1);
          if ((localRectangle.width > 0) && (localRectangle.height > 0) && (localRectangle.x == i) && (localRectangle.y == j))
          {
            this.nextrow = j;
            this.nextcol = i;
            this.next = localCell;
            return;
          }
          ++i;
        }
        i = this.columnFrom;
      }
      label199: this.next = null;
    }

    IntersectIterator(, int paramInt1, int paramInt2, int paramInt3, int paramInt4, DefaultCellCase.1 param1)
    {
      this(paramDefaultCellCase, paramInt1, paramInt2, paramInt3, paramInt4);
    }
  }

  private class CellIterator
    implements Iterator
  {
    int thisrow;
    int thiscol;
    int nextrow;
    int nextcol;
    Cell next;
    private final DefaultCellCase this$0;

    CellIterator()
    {
      this.this$0 = paramDefaultCellCase;
      this.thisrow = -1;
      this.thiscol = -1;
      this.nextrow = 0;
      this.nextcol = -1;
      this.next = null;
      findNext();
    }

    public boolean hasNext()
    {
      return (this.next != null);
    }

    public Object next()
    {
      if (!(hasNext()))
        throw new NoSuchElementException("At last element");
      Cell localCell = this.next;
      this.thiscol = this.nextcol;
      this.thisrow = this.nextrow;
      findNext();
      return localCell;
    }

    public void remove()
    {
      this.this$0.removeCell(this.thiscol, this.thisrow);
    }

    private void findNext()
    {
      int i = this.nextcol + 1;
      for (int j = this.nextrow; j < DefaultCellCase.access$100(this.this$0).size(); ++j)
      {
        DefaultCellCase.Row localRow = (DefaultCellCase.Row)DefaultCellCase.access$100(this.this$0).get(j);
        while (i < localRow.width())
        {
          Cell localCell = localRow.getElement(i);
          if ((localCell != null) && (localCell.getColumn() == i) && (localCell.getRow() == j))
          {
            this.nextrow = j;
            this.nextcol = i;
            this.next = localCell;
            return;
          }
          ++i;
        }
        i = 0;
      }
      this.next = null;
    }
  }

  private static class Row
    implements Serializable, Cloneable
  {
    private static final long serialVersionUID = 4518514222514670793L;
    private Cell[] elementData = null;

    Row()
    {
    }

    Row(int paramInt)
    {
      this.elementData = new Cell[paramInt];
    }

    public void setElement(int paramInt, Cell paramCell)
    {
      ensureCapacity(paramInt + 1);
      this.elementData[paramInt] = paramCell;
      if (paramCell == null)
        shrink_array();
    }

    private void shrink_array()
    {
      for (int i = this.elementData.length - 1; i >= 0; --i)
        if (this.elementData[i] != null)
          break;
      if (i + 1 < width())
      {
        Cell[] arrayOfCell = this.elementData;
        this.elementData = new CellElement[i + 1];
        System.arraycopy(arrayOfCell, 0, this.elementData, 0, this.elementData.length);
      }
    }

    public void insert(int paramInt)
    {
      int i = width();
      if ((paramInt >= i) || (paramInt < 0))
        return;
      ensureCapacity(i + 1);
      System.arraycopy(this.elementData, paramInt, this.elementData, paramInt + 1, i - paramInt);
      this.elementData[paramInt] = null;
    }

    public Cell getElement(int paramInt)
    {
      if ((paramInt >= width()) || (paramInt < 0))
        return null;
      return this.elementData[paramInt];
    }

    public void removeColumn(int paramInt)
    {
      int i = width();
      if ((paramInt >= i) || (paramInt < 0))
        return;
      Cell[] arrayOfCell = this.elementData;
      this.elementData = new CellElement[i - 1];
      if (paramInt > 0)
        System.arraycopy(arrayOfCell, 0, this.elementData, 0, paramInt);
      System.arraycopy(arrayOfCell, paramInt + 1, this.elementData, paramInt, this.elementData.length - paramInt);
    }

    public int width()
    {
      return ((this.elementData == null) ? 0 : this.elementData.length);
    }

    public int getNotNullWidth()
    {
      if (this.elementData == null)
        return 0;
      for (int i = this.elementData.length - 1; i >= 0; --i)
        if (this.elementData[i] != null)
          return (i + 1);
      return 0;
    }

    public String toString()
    {
      return "size:" + width();
    }

    private void ensureCapacity(int paramInt)
    {
      if (this.elementData == null)
      {
        this.elementData = new CellElement[paramInt];
      }
      else
      {
        int i = this.elementData.length;
        if (paramInt > i)
        {
          Cell[] arrayOfCell = this.elementData;
          this.elementData = new CellElement[paramInt];
          System.arraycopy(arrayOfCell, 0, this.elementData, 0, arrayOfCell.length);
        }
      }
    }
  }
}